import 'dart:async';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/baseblocs/bloc_provider.dart';
import 'package:shopping_group/http/apis/apis.dart';
import 'package:shopping_group/http/http_request_all.dart';
import 'package:shopping_group/modle/person/account/model/lz_mine_accoun_money_model.dart';
import 'package:shopping_group/modle/person/account/view/lz_mine_account_detail_page.dart';
import 'package:shopping_group/utils/network_utils.dart';
import 'package:shopping_group/utils/object_util.dart';
import 'package:shopping_group/utils/route_util.dart';

class LzMineAccountMoneyBloc implements BlocBase {
  List<LzMineAccounMoneyModel> _listEntity = [];
  String _hasMore = '0';
  int _pageNo = 1;
  ScrollController _scrollController;
  LzAccountStyle accountStyle;
  StreamController<Object> _initController = StreamController<Object>();
  StreamSink get initDate => _initController.sink;

  StreamController<Object> _allController = StreamController<Object>();
  StreamSink get _addDate => _allController.sink;
  Stream<Object> get sendData => _allController.stream;

  //处理more响应
  StreamController<ScrollController> _actionMoreController = StreamController<ScrollController>();
  StreamSink get moreDate => _actionMoreController.sink;

  //处理上拉刷新
  StreamController<ScrollController> _actionRefreshController = StreamController<ScrollController>();
  StreamSink get refreshDate => _actionRefreshController.sink;

  LzMineAccountMoneyBloc() {
    _initController.stream.listen(initData);
    _actionMoreController.stream.listen(loadMoreData);
    _actionRefreshController.stream.listen(refreshData);
  }

  void initData(data) {
    accountStyle = data;
    _listEntity?.clear();
    _pageNo = 1;
    _initTask(_pageNo.toString());
  }

  void refreshData(data) {
    _pageNo = 1;
    _listEntity?.clear();
    _initTask(_pageNo.toString());
  }

  void loadMoreData(controller) {
    if (controller is ScrollController) {
      _scrollController = controller;
      var maxScroll = _scrollController.position.maxScrollExtent;
      var pixel = _scrollController.position.pixels;
      var count = _listEntity.length;
      var length = int.parse(_hasMore);
      if (maxScroll == pixel && count > 0 && count < length) {
        _pageNo++;
        _initTask(_pageNo);
      }
    }
  }

  void _initTask(pageNo) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext).requestFocus(FocusNode());
    Map<String, dynamic> data = {};
    data['val'] = BaseConstant.userId;
    data['pageIndex'] = pageNo;
    data['pageSize'] = BaseConstant.PAGE_SIZE;

    String url = "";
    switch (accountStyle) {
      case LzAccountStyle.myJintie:
        url = Apis.JIN_TIE_LIST;
        break;
      case LzAccountStyle.myZt:
        url = Apis.ZI_TUI_LIST;
        break;
      case LzAccountStyle.myDsFuli:
        url = Apis.DS_FULI_List;
        break;
      case LzAccountStyle.myAdverRights:
        url = Apis.QRY_USER_LOOK_COUNT_LIST;
        break;
      case LzAccountStyle.myDJQ:
        url = Apis.MY_DJQ_ACCOUNT_LIST;
        break;
      case LzAccountStyle.myProductShelf:
        url = Apis.GET_CANUP_MY_COMMO_LIST;
        break;

      case LzAccountStyle.myTeamJT:
        url = Apis.GET_GROUP_JT_OUT_LIST;
        break;

      default:
    }

    //BotToast.showLoading();
    NetworkUtils.isNetworkConnected(connectedCallback: (connected) {
      if (!connected) {
        BotToast.showText(text: '网络无法连接');
      } else {
        HttpUtilAll.post(url, data: data, headers: {}, success: (result) {
          BotToast.closeAllLoading();
          if (null != result) {
            List list = List.from(result['data']);
            if (ObjectUtil.isEmptyList(list)) {
              list = [];
            }
            for (var item in list) {
              _listEntity.add(LzMineAccounMoneyModel.fromMap(item));
            }
            _hasMore = result['total'].toString();
          }
          _addDate.add(_listEntity);
        }, error: (errorMsg) {
          BotToast.closeAllLoading();
          // 请求失败返回的错误信息
          BotToast.showText(text: errorMsg);
        });
      }
    });
  }

  @override
  void dispose() {
    _initController?.close();
    _allController?.close();
    _actionMoreController?.close();
    _actionRefreshController?.close();
  }
}
